package com.ushier.shop.fruits.controller;

import com.ushier.shop.fruits.bean.Fruit;
import com.ushier.shop.fruits.bean.ResponseMessageBean;
import com.ushier.shop.fruits.dao.FruitDAO;
import com.ushier.shop.fruits.util.StringUtil;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@WebServlet("/user/shopping/car")
public class UserShoppingCarServlet extends HttpServlet {

    private FruitDAO fruitDAO = new FruitDAO();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("content-type","text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();

        String idStr = req.getParameter("id");

        String action = req.getParameter("action");

        if(StringUtil.isNull(action)){
            out.print(JSONObject.fromObject(ResponseMessageBean.error("缺少参数")));
            return;
        }

        if(!"list".equals(action)){
            if(StringUtil.isNull(idStr)){
                out.print(JSONObject.fromObject(ResponseMessageBean.error("缺少参数")));
                return;
            }
        }

        List<Fruit> list = null;
        Integer uid = null;
        switch (action){
            case "add":
                String countStr = req.getParameter("count");
                if(StringUtil.isNull(countStr)){
                    out.print(JSONObject.fromObject(ResponseMessageBean.error("缺少参数")));
                    return;
                }

                uid = (Integer) session.getAttribute("uid");
                if(null == uid){
                    out.print(JSONObject.fromObject(ResponseMessageBean.error("请登录后操作")));
                    return;
                }

                Fruit apple = fruitDAO.get(Integer.valueOf(idStr));
                if(null == apple){
                    out.print(JSONObject.fromObject(ResponseMessageBean.error("无此商品")));
                    return;
                }
                apple.setCount(Integer.valueOf(countStr));
                list = (List<Fruit>) session.getAttribute("list_" +uid);
                if(null == list){
                    list = new ArrayList<>();
                }

                boolean alreadyExists = false;
                for(Fruit item : list){
                    if(item.getId() == apple.getId()){
                        item.setCount(item.getCount() + apple.getCount());
                        alreadyExists = true;
                    }
                }

                if(!alreadyExists){
                    list.add(apple);
                }

                session.setAttribute("list_" + uid, list);
                out.print(JSONObject.fromObject(ResponseMessageBean.success()));
                return;
            case "delete":

                uid = (Integer) session.getAttribute("uid");
                if(null == uid){
                    out.print(JSONObject.fromObject(ResponseMessageBean.error("请登录后操作")));
                    return;
                }

                list = (List<Fruit>) session.getAttribute("list_" + uid);
                if(null != list){
                    for(Iterator<Fruit> it = list.iterator(); it.hasNext(); ){
                        Fruit next = it.next();
                        if(Integer.valueOf(idStr).compareTo(next.getId()) == 0){
                            it.remove();
                        }
                    }
                }
                out.print(JSONObject.fromObject(ResponseMessageBean.success()));
                return;
            case "list":
                uid = (Integer) session.getAttribute("uid");
                if(null == uid){
                    out.print(JSONObject.fromObject(ResponseMessageBean.error("请登录后操作")));
                    return;
                }
                list = (List<Fruit>) session.getAttribute("list_" + uid);
                out.print(JSONObject.fromObject(ResponseMessageBean.success(list)));
                return;
            default:
                break;
        }

    }
}
